package scalapackage

/**
  * Created by Germmy on 2018/3/18.
  */
object WordCount {
  def main(args: Array[String]) {

    //目标一长串文章中同样单词的word count

    //构造一个List
    var myList=List("java is a beautiful girl china is so small","china is so big")

    //按空格切分再压平，当然可以直接用flattenMap
    var myList2=myList.map(_.split(" ")).flatten
//    println(myList2)
//    println(myList2.size)

    //执行结果：List(java, is, a, beautiful, girl, china, is, so, small, china, is, so, big)

    //转换为元组，用加1的方式来构造
    var map1=myList2.map((_,1))

//    println(map1)
    //执行结果：List((java,1), (is,1), (a,1), (beautiful,1), (girl,1), (china,1), (is,1), (so,1), (small,1), (china,1), (is,1), (so,1), (big,1))

    //按照元组的第1个元素来分组
    var map2=map1.groupBy(_._1)
//    println(map2)

    //取出map2的values,将它的长度算出来，其实就是单词的个数
    var map3=map2.mapValues(_.size)
//    println(map3)

    //排序,直接转化为list
    var list3=map3.toList.sortBy(_._2)
//    println(list3)

    //降序排列
    var list4=list3.reverse
    println(list4)//List((is,3), (china,2), (so,2), (girl,1), (small,1), (java,1), (a,1), (big,1), (beautiful,1))


  }
}
